const db = require('../mysql/db')
const jsonData = require('./details.json')
const data = jsonData.result

let shopIMGToString = ["http://shop.graceman.com.cn/upload/20140114/20140114115733.jpg", "http://shop.graceman.com.cn/upload/20140114/20140114115739.jpg", "http://shop.graceman.com.cn/upload/20140114/20140114115747.jpg"]
shopIMGToString = JSON.stringify(shopIMGToString);
let detailToString = {
    生产日期: '2020年03月18日',
    品牌: '晚歌',
    系列: '城堡',
    产地: '法国',
    糖分: '干葡萄酒 （含糖量小于4克/升）',
    香味: '果香',
    包装种类: '瓶装',
    商品规格: '6x2x750ml',
    葡萄酒种类: '红葡萄酒',
    葡萄酒等级: '一等品',
    醒酒时间: '15分钟(含)-30分钟(不含)',
    搭配菜肴: '红肉/野味/陈乳酪',
    // 红肉/野味/陈乳酪: '整箱装',
    葡萄品种: '美乐/梅洛 赤霞珠',
    进口类型: '原汁进口',
    储存条件: '常温',
    采摘年份: '2018-10',
    厂名: '法国新阳光海岸酒庄',
    厂址: '法国',
    厂家联系方式: '4000896311',
    配料表: '葡萄汁，二氧化硫',
    储藏方法: '常温，避光，卧放',
    保质期: '10年',
    食品添加剂: '无'
}
detailToString = JSON.stringify(detailToString);
let detailIMGToString = [
    "https://img.alicdn.com/imgextra/i2/1796875493/O1CN01jalFYM1qRqPLhASs8_!!1796875493.jpg",
    "https://img.alicdn.com/imgextra/i3/1796875493/O1CN01VHtQ3X1qRqOAIPnHR_!!1796875493.jpg",
    "https://img.alicdn.com/imgextra/i1/1796875493/O1CN01pH8fma1qRqPTKZuz8_!!1796875493.jpg",
    "https://img.alicdn.com/imgextra/i2/1796875493/O1CN01ssYW8C1qRqMWjixj2_!!1796875493.jpg",
    "https://img.alicdn.com/imgextra/i1/1796875493/O1CN012DdPcd1qRqNzYFM5O_!!1796875493.jpg"
]
detailIMGToString = JSON.stringify(detailIMGToString);
let commentsToString = [
    {
        "userIcon": "http://sns.m.taobao.com/avatar/sns/user/flag/sns_logo?type=taobao&kn=wwc_tb_11&bizCode=taobao_avatar&userFlag=RAzN84GK7wS8eNduDntv2nDbfvYtZaZj519os2JqKHygEiWfFDeGEMqHBTr4x9ZZn7AEA3AQtrruCBFA5eKdiCZUbp6bTUuSbd2GsXkKMPWVhjBaT3e8ChxYsykAuNh1cADN13PTBTPCJaGWFc2rjfy7nD7RhrkbuioAbS1",
        "userName": "星光没你灿烂",
        "userTime": "1个月前",
        "shopCategory": "13瓶帕拉马",
        "userCommont": "红酒一如既往的正品，好喝，每年都要买几箱，这是今年的第四箱啦。还送了专业的倒酒器和瓶盖，方便随时平常。",
        "shopCommentsIMG": [
            "https://gw.alicdn.com/imgextra/i4/O1CN01X0cJpK1h3Gc5s9bUD_!!0-rate.jpg_110x10000q90.jpg_.webp",
            "https://gw.alicdn.com/imgextra/i3/O1CN01PIi2iL1h3GcDBfSc1_!!0-rate.jpg_110x10000q90.jpg_.webp",
            "https://gw.alicdn.com/imgextra/i3/O1CN014o2LCt1s8GiI44d4s_!!0-rate.jpg_110x10000q90.jpg_.webp"
        ],
        "reply": "感谢您对雍天的支持与认可，我们会不断努力，带来更多高性价比的优质好酒以及更细心更便捷的服务,期待您的下次光临哦！",
        "views": 368
    },
    {
        "userIcon": "http://img.alicdn.com/tps/TB1l6dkOXXXXXXEXVXXXXXXXXXX-210-210.png_70x70.jpg",
        "userName": "汤师爷",
        "userTime": "18天前 ",
        "shopCategory": "13瓶帕拉马",
        "userCommont": "红酒已经收到，宝贝完好无损，为掌柜和快递点赞，用餐巾纸法和小苏打法检测，红酒很好。刮开防伪码检测---通过。红酒口感不错，感觉比路易拉菲要好，真的谢谢！很好",
        "shopCommentsIMG": [],
        "reply": "",
        "views": 204
    }
]
commentsToString = JSON.stringify(commentsToString);

dbInsert()
function dbInsert() {
    // // 正常写入，不能写入对象以及数组
    // const sqlInsert = `
    // insert into details (id,shopName,shopPicture,babyDescription,sellerService,logisticsService, 
    //     commodityName, sales, price, nowPrice) values (?,?,?,?,?,?,?,?,?,?)
    // `
    // db.query(sqlInsert,
    //     [
    //         data.id,
    //         data.shopName,
    //         data.shopPicture,
    //         data.babyDescription,
    //         data.sellerService,
    //         data.logisticsService,
    //         data.commodityName,
    //         data.sales,
    //         data.price,
    //         data.nowPrice,
    //         data.detail,
    //         data.detailIMG,
    //         data.comments
    //     ], (err, results) => {
    //         // 执行SQL语句失败
    //         if (err) return console.log(err.message);
    //         // 成功了
    //         if (results.affectedRows === 1) {
    //             console.log("插入数据成功！");
    //         }
    //         return
    //     })

    // // 正常写入对象到一个属性下
    // db.query('INSERT INTO details SET detail = ?', detailToString, function (error, results, fields) {
    //     if (error) throw error;
    //     console.log('Inserted comment with ID:', results.insertId);
    // });

    db.query(`
    INSERT INTO details SET 
    category = ?,id = ?, shopName = ?, shopPicture = ?, babyDescription = ?, sellerService = ?, 
    logisticsService = ?,shopIMG = ?, commodityName = ?, sales = ?, price = ?, nowPrice = ?, 
    detail = ?, detailIMG = ?, comments = ?
    `, [
        data.category,
        data.id,
        data.shopName,
        data.shopPicture,
        data.babyDescription,
        data.sellerService,
        data.logisticsService,
        // data.shopIMG,
        shopIMGToString,
        data.commodityName,
        data.sales,
        data.price,
        data.nowPrice,
        // data.detail,
        // data.detailIMG,
        // data.comments,
        detailToString,
        detailIMGToString,
        commentsToString
    ], function (error, results, fields) {
        // 执行SQL语句失败
        if (error) return console.log(error.message);
        // 成功了
        if (results.affectedRows === 1) {
            console.log("插入数据成功！");
        }
        db.end()
        return
    });
}
